package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 反馈页面
 * 
 * @author star
 * 
 */
public class FeedBackActivity extends Activity {

	private Button back;
	private EditText feedBackText;
	private EditText qqOrEmail;
	private Button btnSubmitFeedBack;
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private final static int TIMEOUT=9999;
	private Thread thread;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				TipsUtil.showAlertFinish(
						FeedBackActivity.this, null,
						"问题反馈成功！您将收到来自“健康护照”官方的权威回答！");
				break;
			case FAILE:
				TipsUtil.showAlert(
						FeedBackActivity.this, null,
						"反馈问题失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(
						FeedBackActivity.this, null,
						"未知错误！");
				break;
			default:
				break;
			}

		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.feedback);
		initPages();
	}

	private void initPages() {

		back = (Button) findViewById(R.id.btnBack);
		btnSubmitFeedBack = (Button) findViewById(R.id.btnSubmitFeedBack);
		feedBackText = (EditText) findViewById(R.id.etFeedBack);
		qqOrEmail = (EditText) findViewById(R.id.etContact);
		back.setOnClickListener(clickListener);
		btnSubmitFeedBack.setOnClickListener(clickListener);
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			case R.id.btnSubmitFeedBack:
				if (feedBackText.getText() != null
						&& feedBackText.getText().toString().length() > 0
						&& qqOrEmail.getText() != null
						&& qqOrEmail.getText().toString().length() >= 4) {
					getData();
				
				} else {
					Toast.makeText(FeedBackActivity.this, "输入有误",
							Toast.LENGTH_LONG).show();
				}
				break;
			default:
				break;
			}
		}

	};
	
	private void getData(){
		
		
		thread=new Thread(new Runnable() {
			public void run() {
				new RequestParamtersUtil().FeedbackContent(
						FeedBackActivity.this, feedBackText.getText()
								.toString(),
						qqOrEmail.getText().toString(),
						new DefaultJSONData() {

							@Override
							public void parse(JSONObject object) {
								Log.e("FeedbackContent", object.toString());
							}

							@Override
							public void parse(JSONArray array) {
								Log.e("FeedbackContent", array.toString());
							}

							@Override
							public void parse(String result)
									throws JSONException {
								Log.e("FeedbackContent", result);
								if ("true".equals(result)) {
									handler.sendEmptyMessage(SUCCESS);
								} else if("false".equals(result)){
									handler.sendEmptyMessage(FAILE);
									
								}else{
									handler.sendEmptyMessage(ERROR);
									
								}

							}
						});
			}
		});
	thread.start();
	}
}
